/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package detplagiasi;

import java.awt.Cursor;
import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author vebry3
 */
public class DetPlagGUI extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */

    static File dataset, doc, output,a;
    String datasetPath, docPath, outPath;
    static File[] fileC;
    static String runningTime;
    long startTime = 0, endTime = 0, elapsed, hours, minutes, seconds;
    
    Container container = new Container();
    Clustering clusterer = new Clustering();
    

    public DetPlagGUI() {
        initComponents();
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        AlgoGrp = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        datasetLoc = new javax.swing.JTextField();
        browseDataset = new javax.swing.JButton();
        docLoc = new javax.swing.JTextField();
        browseDoc = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        em = new javax.swing.JRadioButton();
        kmeans = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        outputLoc = new javax.swing.JTextField();
        browseOutput = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        processB = new javax.swing.JButton();
        resetB = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("PLAGIARISM DETECTION");

        browseDataset.setText("Browse");
        browseDataset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseDatasetActionPerformed(evt);
            }
        });

        browseDoc.setText("Browse");
        browseDoc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseDocActionPerformed(evt);
            }
        });

        jLabel2.setText("Select your training dataset");

        jLabel3.setText("Select the document to be tested");

        jLabel4.setText("Choose Algorithm");

        AlgoGrp.add(em);
        em.setText("EM Algorithm");

        AlgoGrp.add(kmeans);
        kmeans.setText("K-Means Algorithm");

        jLabel5.setText("Select your output location");

        browseOutput.setText("Browse");
        browseOutput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseOutputActionPerformed(evt);
            }
        });

        jLabel6.setText("Description: Checking a document of an unknown author");

        processB.setText("Process");
        processB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                processBActionPerformed(evt);
            }
        });

        resetB.setText("Reset");
        resetB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetBActionPerformed(evt);
            }
        });

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(datasetLoc)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(browseDataset))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(outputLoc, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(docLoc, javax.swing.GroupLayout.Alignment.LEADING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(browseOutput))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(browseDoc))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(em)
                                        .addGap(36, 36, 36)
                                        .addComponent(kmeans))
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel6)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(74, 74, 74)
                                        .addComponent(processB)
                                        .addGap(65, 65, 65)
                                        .addComponent(resetB, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(69, 69, 69)
                                        .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 75, Short.MAX_VALUE)))
                        .addGap(50, 50, 50))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addGap(57, 57, 57)
                .addComponent(jLabel2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(datasetLoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseDataset))
                .addGap(30, 30, 30)
                .addComponent(jLabel3)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(docLoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseDoc))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(em)
                    .addComponent(kmeans))
                .addGap(10, 10, 10)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputLoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseOutput))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(processB)
                    .addComponent(resetB)
                    .addComponent(btnExit))
                .addContainerGap(69, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void browseDatasetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseDatasetActionPerformed
        JFileChooser datasetCh = new JFileChooser();
        datasetCh.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int nilai = datasetCh.showOpenDialog(this);
        if(nilai==JFileChooser.APPROVE_OPTION){
            dataset = datasetCh.getSelectedFile();
            datasetLoc.setText(dataset.getAbsolutePath());
            datasetPath = (dataset.getAbsolutePath());

        }else{
            datasetLoc.setText("");
        }
    }//GEN-LAST:event_browseDatasetActionPerformed

    private void browseDocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseDocActionPerformed
        JFileChooser docCh = new JFileChooser();
        docCh.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int nilai = docCh.showOpenDialog(this);
        if(nilai==JFileChooser.APPROVE_OPTION){
            doc = docCh.getSelectedFile();
            docLoc.setText(doc.getAbsolutePath());
            docPath = (doc.getAbsolutePath());

        }else{
            docLoc.setText("");
        }
    }//GEN-LAST:event_browseDocActionPerformed

    private void browseOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseOutputActionPerformed
        JFileChooser outputCh = new JFileChooser();
        outputCh.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int nilai = outputCh.showOpenDialog(this);
        if(nilai==JFileChooser.APPROVE_OPTION){
            output = outputCh.getSelectedFile();
            outputLoc.setText(output.getAbsolutePath());
            outPath = (output.getAbsolutePath());

        }else{
            outputLoc.setText("");
        }
    }//GEN-LAST:event_browseOutputActionPerformed

    private void processBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_processBActionPerformed
        
        super.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        //s.start();
        startTime = System.currentTimeMillis();
        prosesApp();
        endTime = System.currentTimeMillis();
        //s.stop();
        super.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        runningTime = getElapsedTime();
        ResultFrame rf = new ResultFrame();
        rf.setVisible(true);
        
        
    }//GEN-LAST:event_processBActionPerformed

    public void prosesApp(){
        if(!"".equals(datasetLoc.getText()) && (!"".equals(docLoc.getText())) &&(!"".equals(outputLoc.getText()))){
            if((em.isSelected()) || (kmeans.isSelected())){
                
            
                try {
                    File folder, ax;
                    folder = new File(datasetLoc.getText());
                    Container.setAddress(outPath);
                    File[] file = folder.listFiles();
                    File[] file2 = null;
                    System.out.println(file.length);
                    System.out.println(datasetPath);
                    System.out.println(docPath);
                    a = Container.docUji(doc);
                    System.out.println(a);
                    if(file.length!=0){
                        for (File file1 : file) {
                            if (file1.isFile()) {
                                Container.simpanFile(file1);
                                System.out.println("condition if is used");
                            } else if (file1.isDirectory()) {
                                ax = new File(file1.getAbsolutePath());
                                System.out.println("folder name : "+ax);
                                file2 = file1.listFiles();
                                for(int l=0; l<file2.length;l++){
                                    System.out.println("file ke "+(l+1)+":"+ file2[l]);
                                    Container.simpanFile(file2[l]);
                                    System.out.println("condition if else is used");
                                }
                            }
                        }
                        if(file2!=null){
                            //jika terdapat folder di dalam folder,menggabungkan semua file yg terdapat di dalamnya ke dalam array baru fileC.
                            fileC = new File[file.length + file2.length];
                            System.arraycopy(file, 0, fileC, 0, file.length);
                            System.arraycopy(file2, 0, fileC, file.length, file2.length);
                            for(int ss=0;ss<fileC.length;ss++){
                                System.out.println("ini isi fileC"+fileC[ss]);
                            }
                        }else{ //jika tidak ada folder dalam folder
                            fileC= new File[file.length];
                            for(int ss=0;ss<file.length;ss++){
                                System.out.println("ini isi fileC"+fileC[ss]);
                            }
                        }

                        if(em.isSelected()){
                            clusterer.method=1;
                            ResultFrame.setMethod("EM Algorithm");
                            clusterer.startCluster(fileC);
                            //a =clusterer.td.fileName[1];
                        }else if(kmeans.isSelected()){
                            clusterer.method=2;
                            ResultFrame.setMethod("Simple KMeans Algorithm");
                            clusterer.startCluster(fileC);
                        }
                    }else{
                        JOptionPane.showMessageDialog(null, "Folder is empty", "Error", JOptionPane.ERROR_MESSAGE);
                    }

                } catch (Exception ex) {
                    //System.out.println(files.length);
                    Logger.getLogger(DetPlagGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }else{
                JOptionPane.showMessageDialog(null, "Please choose your algorithm", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }else{
            JOptionPane.showMessageDialog(null,  "Silahkan lengkapi semua field terlebih dahulu.", "Input Kosong", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void resetBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetBActionPerformed
        datasetLoc.setText("");
        docLoc.setText("");
        outputLoc.setText("");
    }//GEN-LAST:event_resetBActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btnExitActionPerformed
    public String getElapsedTime() {
        elapsed = (endTime - startTime);
        hours = TimeUnit.MILLISECONDS.toHours(elapsed);
        elapsed -= TimeUnit.HOURS.toMillis(hours);
        minutes = TimeUnit.MILLISECONDS.toMinutes(elapsed);
        elapsed -= TimeUnit.MINUTES.toMillis(minutes);
        seconds = TimeUnit.MILLISECONDS.toSeconds(elapsed);
        StringBuilder sb = new StringBuilder(64);
        sb.append(hours);
        sb.append(" Hours ");
        sb.append(minutes);
        sb.append(" Minutes ");
        sb.append(seconds);
        sb.append(" Seconds");

        return(sb.toString());
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DetPlagGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DetPlagGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DetPlagGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DetPlagGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DetPlagGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup AlgoGrp;
    private javax.swing.JButton browseDataset;
    private javax.swing.JButton browseDoc;
    private javax.swing.JButton browseOutput;
    private javax.swing.JButton btnExit;
    private javax.swing.JTextField datasetLoc;
    private javax.swing.JTextField docLoc;
    private javax.swing.JRadioButton em;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JRadioButton kmeans;
    private javax.swing.JTextField outputLoc;
    private javax.swing.JButton processB;
    private javax.swing.JButton resetB;
    // End of variables declaration//GEN-END:variables
}
